package me.touch.knight.practices;


import me.touch.knight.practices.loader.JarLauncher;
import me.touch.knight.practices.loader.jar.JarFile;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * Hello world!
 */
public class Application extends JarLauncher{

    public Application () throws Exception {
        JarFile.registerUrlProtocolHandler();
        ClassLoader classLoader = createClassLoader(getClassPathArchives());
        Thread.currentThread().setContextClassLoader(classLoader);
        URL[] urls = ((URLClassLoader)classLoader).getURLs();
        for(URL url : urls){
            System.out.println(url.getPath());
        }
    }

    public void sayHello(String name) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass("me.touch.knight.practices.HelloWorld");
        Method mainMethod = clazz.getDeclaredMethod("sayHello", String.class);
        mainMethod.invoke(clazz.newInstance(), "yyd");
//        HelloWorld helloWorld = new HelloWorld();
//        helloWorld.sayHello(name);name
    }
}
